var UserLogon = (function() {
	var user = null;
	var userLogonObservers = [];
	
	var login = function(accountkey) {
		var url = {
				type: "user",
				method: "login",
		};
		
		var params = {
				url: url,
				post: { accountkey: accountkey },
				success : function(data) {
//					loader.load(data);
					user = data;
					notifyLogonState(true);
				},
				error : function(e) {
					console.log("error");
					console.log(e);
					notifyLogonState(false);
				}
		};
		
		Request.send(params);
	};
	
	var logout = function() {
		user = null;
		notifyLogonState(false);
	};
	
	var getUserKey = function() {
		if(user != null){
			return user.userkey;
		}
		
		return "";
	};
	
	var registerUserLogonObserver = function(observer) {
		userLogonObservers.push(observer);
	};
	
	var notifyLogonState = function(state) {
		$.each(userLogonObservers, function(i, observer) {
			observer.notify(state, user);
		});
	};
	
	return {
		isLoggedIn: user != null,
		login: login,
		logout: logout,
		getUser: user,
		getUserKey: getUserKey,
		registerUserLogonObserver: registerUserLogonObserver
	};
})();